package com.strato.logging.saas.v1_0_0;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.strato.logging.v1_0_0.api.Log;
import com.strato.mvc.api.v1_0_0.user.UserContext;
import com.strato.saas.merchant.v1_0_0.MerchantContextHolder;

public class SaasLog implements Log {

	private String caninonName;
	private Log syslog;
	
	public SaasLog(String name, Log syslog) {
		this.syslog=syslog;
		int index=name.lastIndexOf(".");
		if(index==-1){
			caninonName=name;
		}else{
			caninonName=name.substring(index);
		}
	}

	private void output(String level,Object message, Throwable e) {
		String merchantId=MerchantContextHolder.get();
		StringBuffer buf=new StringBuffer();
		DateFormat format=new SimpleDateFormat("MM-dd HH:mm:ss.SSS");
		buf.append(format.format(new Date()));
		buf.append("[").append(level).append("]");
		buf.append(Thread.currentThread().getName()).append("\t");
		buf.append(UserContext.get()).append("\t");
		buf.append(caninonName).append("\t");
		buf.append(message).append("\n");
		OutputManager.getInstance().output(merchantId, buf.toString(), e);
	}
	
	@Override
	public void trace(Object message) {
		if(syslog.isTraceEnabled()){
			syslog.trace(message);
		}
		if(isTraceEnabled()){
			output("TRACE",message,null);
		}
	}

	@Override
	public void trace(Object message, Throwable e) {
		if(syslog.isTraceEnabled()){
			syslog.trace(message, e);
		}
		if(isTraceEnabled()){
			output("TRACE",message,e);
		}
	}

	@Override
	public void debug(Object message) {
		if(syslog.isDebugEnabled()){
			syslog.debug(message);
		}
		if(isDebugEnabled()){
			output("DEBUG",message,null);
		}
	}

	@Override
	public void debug(Object message, Throwable e) {
		if(syslog.isDebugEnabled()){
			syslog.debug(message, e);
		}
		if(isDebugEnabled()){
			output("DEBUG",message,e);
		}
	}

	@Override
	public void info(Object message) {
		if(syslog.isInfoEnabled()){
			syslog.info(message);
		}
		if(isInfoEnabled()){
			output("INFO",message,null);
		}
	}
	
	@Override
	public void info(Object message, Throwable e) {
		if(syslog.isInfoEnabled()){
			syslog.info(message, e);
		}
		if(isInfoEnabled()){
			output("INFO",message,e);
		}
	}

	@Override
	public void warn(Object message) {
		syslog.warn(message);
		output("WARN",message,null);
	}

	@Override
	public void warn(Object message, Throwable e) {
		syslog.warn(message, e);
		output("WARN",message,e);
	}

	@Override
	public void error(Object message) {
		syslog.error(message);
		output("ERROR",message,null);
	}

	@Override
	public void error(Object message, Throwable e) {
		syslog.error(message, e);
		output("ERROR",message,e);
	}

	@Override
	public void fatal(Object message) {
		syslog.fatal(message);
		output("FATAL",message,null);
	}

	@Override
	public void fatal(Object message, Throwable e) {
		syslog.fatal(message, e);
		output("FATAL",message,e);
	}

	@Override
	public boolean isTraceEnabled() {
		return true;
	}

	@Override
	public boolean isDebugEnabled() {
		return true;
	}

	@Override
	public boolean isInfoEnabled() {
		return true;
	}

}
